﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Observer<UIEventType>
{
    private Dictionary<UIEventType, List<IObserver<UIEventType>>> dic_listeners = new Dictionary<UIEventType, List<IObserver<UIEventType>>>();
    /// <summary>
    /// 增加订阅者
    /// </summary>
    public void AddListener(IObserver<UIEventType> listener, params UIEventType[] eventType)
    {
        for (int i = 0; i < eventType.Length; i++)
        {
            if (dic_listeners.ContainsKey(eventType[i]))
            {
                dic_listeners[eventType[i]].Add(listener);
            }
            else
            {
                List<IObserver<UIEventType>> observers = new List<IObserver<UIEventType>>();
                observers.Add(listener);
                dic_listeners.Add(eventType[i], observers);
            }
        }
    }
    public void RemoveListener(IObserver<UIEventType> listener, UIEventType eventType)
    {
        List<IObserver<UIEventType>> list = dic_listeners[eventType];
        list.Remove(listener);
    }

    public void RemoveListener(IObserver<UIEventType> listener)
    {
        foreach (var item in dic_listeners)
        {
            if (item.Value == listener)
            {
                item.Value.Remove(listener);
            }
        }
    }

    public void Send(UniEventArgs<UIEventType> arg)
    {
        if (dic_listeners.ContainsKey(arg.eventType))
        {
            List<IObserver<UIEventType>> list_autoRemove = null;
            List<IObserver<UIEventType>> subscription = dic_listeners[arg.eventType];
            for (int i = 0; i < subscription.Count; i++)
            {
                if (subscription[i] == null)
                {
                    list_autoRemove = list_autoRemove ?? new List<IObserver<UIEventType>>();
                    list_autoRemove.Add(subscription[i]);
                    continue;
                }
                subscription[i].EventHandler(arg);
            }
            if (list_autoRemove != null)
            {
                for (int i = list_autoRemove.Count - 1; i >= 0; i--)
                {
                    RemoveListener(list_autoRemove[i]);
                }
            }
        }
    }

    /// <summary>
    /// 简单重载Send
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="eventType"></param>
    public void Send(Component sender, UIEventType eventType)
    {
        UniEventArgs<UIEventType> arg = new UniEventArgs<UIEventType>(sender, eventType);
        Send(arg);
    }

    public void Send(Component sender, UIEventType eventType, object content)
    {
        UniEventArgs<UIEventType> arg = new UniEventArgs<UIEventType>(sender, eventType, content);
        Send(arg);
    }
}

public class UniEventArgs<UIEventType>
{
    public readonly Component sender;
    public readonly UIEventType eventType;
    public readonly System.Object content;
    public UniEventArgs(Component SENDER, UIEventType EVENTTYPE)
    {
        sender = SENDER;
        eventType = EVENTTYPE;
    }
    public UniEventArgs(Component SENDER, UIEventType EVENTTYPE, System.Object CONTENT) : this(SENDER, EVENTTYPE)
    {
        content = CONTENT;
    }
}